/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package HumanCentredSystemDesign;

import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author Tim Archer
 */
public class Reservation {


    private Date reservationDate;
    private String itemIdentifier;
    private String libraryMemberID;

    public Reservation(){}
    public Reservation( Date _reservationDate, String _itemIdentifier, String _libraryMemberID){
        this.reservationDate = _reservationDate;
        this.itemIdentifier = _itemIdentifier;
        this.libraryMemberID = _libraryMemberID;
        
    }
    
    public Date getReservationDate(){
        return this.reservationDate;
    }
    public void setReservationDate(Date _reservationDate){
        this.reservationDate = _reservationDate;
    }
    public String getItemIdentifier(){
        return this.itemIdentifier;
    }
    public void setItemIdentifier(String _itemIdentifier){
        this.itemIdentifier = _itemIdentifier;
    }
    public String getlibraryMemberID(){
        return this.libraryMemberID;
    }
    public void setLibraryMemberID(String _libraryMemberID){
        this.libraryMemberID = _libraryMemberID;
    }
   
    public Item getReservedItem(){
        ArrayList<Item> reservedItems;
        Connect myConn = new Connect();
        myConn.establishConnection();
        
        try
        {
            reservedItems = myConn.getBooksFromDatabase(itemIdentifier, "", "", "", "");
            ArrayList<Item> reservedP = myConn.getPeriodicalsFromDatabase(itemIdentifier, "", "", "", "", "");
            for (Item i : reservedP)
            {
                reservedItems.add(i);
            }
            myConn.closeConnection();
            return reservedItems.get(0);
        } 
        catch(Exception err)
        {
            System.out.println("Cannot get Items Associated with this reservation: "  + err.toString());
            myConn.closeConnection();
            return null; 
        }
    }
    
    public Borrower getBorrower(){
        ArrayList<Borrower> borrowerList = new ArrayList<Borrower>();
        Borrower myBorrower = new Borrower();
        Connect conn = new Connect();
        conn.establishConnection();
        borrowerList = conn.getBorrowersFromDatabase(this.libraryMemberID,"","","");
        myBorrower = borrowerList.get(0);
        
        return myBorrower;
    }
    
    public void removeReservationFromDatabase(){
        Connect myConn = new Connect();
        myConn.establishConnection();
        myConn.CancelReservationFromDatabase(this);
        myConn.closeConnection();
        
    }
    public void addReservationToDatabase(){
        Connect myConn  = new Connect();
        myConn.establishConnection();
        myConn.InsertReservationToDatabase(this);
        myConn.closeConnection();
    }
    
    @Override
    public String toString(){
        return "Reservation: " +reservationDate +" "+ itemIdentifier +" " + libraryMemberID ;
    }

}
